
Start with a
   list of type a,
   initial value of type b,
   two functions, f and g. with two inputs

   [x0, x1, x2, x3, x4] :: [a]
   z :: b
   f :: a -> b -> b
   g :: b -> a -> b

We want to understand how the following applications of
foldr and foldl work.

   foldr f z [x0, x1, x2, x3, x4]
   foldl f z [x0, x1, x2, x3, x4]


Here is how foldr uses the function f to "fold" the list.

     f(__, f(__, f(__, f(__, f(__, z)))))

The empty slots are where the elements from the list are placed.

     f(x0, f(x1, f(x2, f(x3, f(x4, z)))))

Here is the code that implements foldr.

    foldr :: (a -> b -> b) -> b -> [a] -> b
    foldr f z [] = z
    foldr f z (x:xs) = f x (foldr f z xs)


If the function f is thought of as an infix operator @,
here is what foldr looks like in operator notation.

      __ @ (__ @ (__ @ (__ @ (__ @ z))))

      x0 @ (x1 @ (x2 @ (x3 @ (x4 @ z))))

We can also represent foldr using a tree. Notice how this
tree grows to the right. (This tree is evaluated from the
bottom up.)
              f
             / \
           x0   f
               / \
             x1   f
                 / \
               x2   f
                   / \
                 x3   f
                     / \
                   x4    z

Important: In the operator notation for foldr, use cons
as the operator and the empty list as the initial value.

      x0 : (x1 : (x2 : (x3 : (x4 : []))))

Notice how, in this case, foldr just rebuilds the original list.
This shows that foldr is a way of generalizing the building, or
walking, of a list.

Here is what foldr1 looks like. It uses the right hand
element of a non-empty list as its "initial" value.

     f(x0, f(x1, f(x2, f(x3, x4))))

              f
             / \
           x0   f
               / \
             x1   f
                 / \
               x2   f
                   / \
                 x3   x4

Here is the code that implements foldr1.

    foldr1 :: (a -> a -> a) -> [a] -> a
    foldr1 f [x] = x
    foldr1 f (x:xs) = f x (foldr1 f xs)


Here is how foldl uses the function g to "fold" the list.

     g(g(g(g(g(z, __), __), __), __), __)

The empty slots are where the elements from the list are placed.

     g(g(g(g(g(z, x0), x1), x2), x3), x4)

Here is the code that implements foldl.

    foldl :: (b -> a -> b) -> b -> [a] -> b
    foldl g z [] = z
    foldl g z (x:xs) = foldl g (g z x) xs


If the function g is thought of as an infix operator @,
here is what foldl looks like in operator notation.

      ((((z @ __) @ __) @ __) @ __) @ __

      ((((z @ x0) @ x1) @ x2) @ x3) @ x4

We can also represent foldl using a tree. Notice how this
tree grows to the left.

                 g
                / \
               g   x4
              / \
             g   x3
            / \
           g   x2
          / \
         g   x1
        / \
      z    x0


Here is what foldl1 looks like. It uses the left hand
element of a non-empty list as its "initial" value.

     g(g(g(g(x0, x1), x2), x3), x4)

                 g
                / \
               g   x4
              / \
             g   x3
            / \
           g   x2
          / \
        x0   x1

Here is the code that implements foldl1.

    foldl1 :: (a -> a -> a) -> [a] -> a
    foldl1 g [x] = x
    foldl1 g (x:y:ys) = foldl1 g ((g x y):ys)
